package Test.Test02;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class SplitListByWeight {
    public static <T> List<T>[] splitListByWeight(List<T> originalList, List<Integer> weights) {
        if (weights.size() != originalList.size()) {
            throw new IllegalArgumentException("The size of weights must be the same as the size of the original list.");
        }

        List<T> list1 = new ArrayList<>();
        List<T> list2 = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < originalList.size(); i++) {
            int weight = weights.get(i);
            if (random.nextInt(weight + 1) <= 2) {
                list1.add(originalList.get(i));
            } else {
                list2.add(originalList.get(i));
            }
        }

        return new List[]{list1, list2};
    }

    public static void main(String[] args) {
        List<Integer> originalList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        List<Integer> weights = Arrays.asList(2, 3, 2, 3, 2, 3, 2, 3, 2, 3); // 假设每个元素的权重分别是2和3

        List<Integer>[] splitLists = splitListByWeight(originalList, weights);
        System.out.println("List 1: " + splitLists[0]);
        System.out.println("List 2: " + splitLists[1]);
    }
}
